<template>
<div id="meta-index">
  <v-header title="元数据索引" :showDownloadBtn = "download" :saveShow = "save"></v-header>
  <el-container class="table-manage-box">
    <el-header class="header-tool-bar">
      <!-- 按钮 -->
      <el-row>
        <el-button type="primary" size="mini" @click="adddialogForm">增加</el-button>
        <el-button type="primary" size="mini" @click.stop="removedata">删除</el-button>
        <el-button type="primary" size="mini" @click="handleEdit">编辑</el-button>
        <el-button type="primary" size="mini" @click="refresh">刷新</el-button>
        <span class="condition-query">
          <el-input v-model="condition" size="mini" prefix-icon="el-icon-search" placeholder="请输入内容"
                    clearable></el-input>
          <el-button type="primary" size="mini" @click="queryForPage">查询</el-button>
        </span>
      </el-row>
    </el-header>
    <el-main class="main-table-box">
      <!-- 表单 -->
      <el-table
        ref="table"
        :data="tableData"
        @selection-change="handleSelectionChange"
        @select="checkrow"
        v-model="checked"
        v-loading="tableLoading"
        tooltip-effect="dark"
        style="width: 100%">
        <el-table-column
          type="selection"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="meta_table_name"
          label="元数据表名"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="meta_table_name_cns"
          label="元数据表中文名"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="meta_type"
          label="元数据类型"
          width="auto"
          :formatter="fmtmetatype">
        </el-table-column>
        <el-table-column
          prop="meta_id_fld"
          label="元代码对应取值字段"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="meta_name_fld"
          label="元名称对应取值字段"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="meta_id_fix"
          label="默认编码前缀"
          width="auto">
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount">
        </el-pagination>
    </el-main>
  </el-container>
  <!-- 新增/编辑-弹窗 -->
    <el-dialog :title="addRoleDialogMode === 'ADD' ? '新增数据' : '编辑数据'"
               :visible.sync="addRoleDialogVisiable"
               v-model="addRoleDialogMode"
               @close="cancel"
               >
      <el-form :model="ruleForm" :rules="rules"  ref="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="元数据表名" prop="meta_table_name">
          <el-input v-model="ruleForm.meta_table_name" :disabled="editmetaid"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="元代码对应取值字段" prop="meta_id_fld">
          <el-input v-model="ruleForm.meta_id_fld"
          :disabled="editsysid" ></el-input>
        </el-form-item>
        </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="元数据表中文名" prop="meta_table_name_cns">
            <el-input v-model="ruleForm.meta_table_name_cns"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="元数据类型" prop="meta_type">
              <el-select v-model="ruleForm.meta_type" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
           <el-form-item label="默认编码前缀" prop="meta_id_fix">
           <el-input v-model="ruleForm.meta_id_fix"></el-input>
           </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技术描述" prop="tech_desc">
            <el-input v-model="ruleForm.tech_desc" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="业务描述" prop="busi_desc">
            <el-input v-model="ruleForm.busi_desc" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="元名称对应取值字段" prop="meta_name_fld">
           <el-input v-model="ruleForm.meta_name_fld" class="R"></el-input>
           </el-form-item>
          </el-col>
        </el-row>
        <el-form-item id="dialogbtn">
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="cancel('ruleForm')">关闭</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
</div>
</template>

<script>
  import ajax from '../../../../api/ajax/ajax'
  import { VHeader } from '../../../../components'
  import api from '../../../../api/url'
  // let clearNewIndicator = vm => {
  //   vm.date = ''
  // }
export default{
  name: 'meta-index',
  components: {
    VHeader
  },
  data () {
      return {
        options: [{
          value: 'elem',
          label: '数据元'
        },
        {
          value: 'table',
          label: '物理模型'
        },
        {
          value: 'sys',
          label: '系统标识'
        },
        {
          value: 'colume',
          label: '应用元'
        },
        {
          value: 'tgt',
          label: '业务指标'
        }
        ],
        tableLoading: true,
        condition: '', // 输入条件
        addRoleDialogVisiable: false,
        editdialogform: false,
        currentPage: 1, // 当前页码
        pagesize: 10, // 默认每页数据量
        totalCount: 0, // 默认数据总数
        editmetaid: false, // meta_id input框禁止修改
        editsysid: false, // event_id input框禁止修改
        checked: false,
        tableData: [],
        checkbox: [],
        addRoleDialogMode: 'ADD', // 对话框色对话框模式  ADD新增 MODIFY 修改
        list: [],
        download: false,
        save: false,
        ruleForm: {
          meta_table_name: '',
          meta_id_fld: '',
          meta_id_fix: '',
          meta_table_name_cns: '',
          meta_type: '',
          busi_desc: '',
          tech_desc: '',
          meta_name_fld: ''
        },
        rules: {
          meta_table_name: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 60, message: '最多输入60个字符', trigger: 'blur' }
          ],
           meta_id_fix: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 3, message: '最多输入3个字符', trigger: 'blur' }
          ],
          meta_table_name_cns: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 20, message: '最多输入20个字符', trigger: 'blur' }
          ],
          meta_type: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 20, message: '最多输入20个字符', trigger: 'blur' }
          ],
          meta_id_fld: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 60, message: '最多输入60个字符', trigger: 'blur' }
          ],
          busi_desc: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 150, message: '最多输入150个字符', trigger: 'blur' }
          ],
          tech_desc: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 150, message: '最多输入150个字符', trigger: 'blur' }
          ],
          meta_name_fld: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 60, message: '最多输入60个字符', trigger: 'blur' }
          ]
        },
        currentItem: {}
      }
    },
    created () {
      this.queryForPage()
    },
    methods: {
      fmtmetatype (row, column) {
        return row.meta_type === 'elem' ? '数据元' : row.meta_type === 'table' ? '物理模型' : row.meta_type === 'sys' ? '系统标识' : row.meta_type === 'colume' ? '应用元' : row.meta_type === 'tgt' ? '业务指标' : '未知类型'
      },
      // 每页显示条目个数
      handleSizeChange (val) {
        this.pagesize = val
        this.handleCurrentChange()
      },
      queryForPage () {
         const pagelistdata = {
          keyword: this.condition,
          pageNo: this.currentPage,
          pageSize: this.pagesize
        }
        ajax.post(api.SELECT_META_INDEX_LIST_FOR_PAGE, pagelistdata).then((res) => {
          this.tableLoading = false
          this.tableData = []
          this.tableData = res.data.list
          // Object.keys(this.tableData).forEach((key) => {
          //   switch (this.tableData[key].meta_type) {
          //     case 'elem':
          //     this.tableData[key].meta_type = '数据元'
          //     break
          //     case 'table':
          //     this.tableData[key].meta_type = '物理模型'
          //     break
          //     case 'sys':
          //     this.tableData[key].meta_type = '系统标识'
          //     break
          //     case 'colume':
          //     this.tableData[key].meta_type = '应用元'
          //     break
          //     case 'tgt':
          //     this.tableData[key].meta_type = '业务指标'
          //     break
          //   }
          // })
          this.totalCount = res.data.total
          this.checked = false // 清除选中状态
        })
      },
      // 当前页数
      handleCurrentChange (val) {
        this.currentPage = val
        this.queryForPage()
      },
      // 增加按钮
      adddialogForm () {
        this.ruleForm = {}
        this.currentItem = {}
        this.$refs.table.clearSelection() // 点击新增清除选中状态
        this.checked = false
        this.addRoleDialogVisiable = true
        this.addRoleDialogMode = 'ADD'
        this.editmetaid = false
        this.editsysid = false
      },
      // 关闭
      cancel (formName) {
        // this.$refs.ruleForm.clearValidate()
        // this.ruleForm = {}
        this.$refs.ruleForm.resetFields() // 移除校验 重置为初始状态
        this.addRoleDialogVisiable = false
        this.editdialogform = false
        this.ruleForm = this.currentItem
        this.checked = false
        this.handleSizeChange()
      },
         // 重置
      resetForm () {
    this.ruleForm = {...this.currentItem}
    this.$refs.ruleForm.clearValidate()
      },
      // 删除数据
      removedata () {
        if (this.list.length > 0) {
          this.$confirm('是否删除该记录?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let remdata = {
              key_list: this.list
            }
            ajax.post(api.DELETE_META_INDEX, remdata).then((res) => {
              this.handleSizeChange()
            })
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }).catch(() => {
            // 去掉取消删除提示
            // this.$message({
            //   type: 'info',
            //   message: '已取消删除'
            // })
          })
        } else {
          this.$message.error('请至少选择一条记录进行删除')
        }
      },
      handleSelectionChange (val) {
        this.list = []
        for (let i = 0; i < val.length; i++) {
          let remdata = {
            meta_table_name: val[i].meta_table_name
          }
          this.list.push(remdata)
        }
      },
      checkrow (row) {
        if (row.length > 0) {
          this.ruleForm = row[0]
          this.checkbox = row
          this.checked = true
        } else {
          this.checked = false
        }
      },
      // 编辑
      handleEdit (row) {
        // 判断是否勾选
        if (this.checked === true) {
          if (this.checkbox.length < 2) {
            this.addRoleDialogVisiable = true // 显示弹框
            this.editmetaid = true // meta_table_name input框禁止修改
            this.editsysid = false // meta_id_fld input框禁止修改
            this.addRoleDialogMode = 'MODIFY' // 新增角色对话框模式 编辑模式
            this.currentItem = JSON.parse(JSON.stringify(this.ruleForm))
            this.ruleForm = JSON.parse(JSON.stringify(this.ruleForm))
          } else {
            this.$message.error('只能选择一条')
          }
        } else {
          this.$message.error('请至少选择一条记录进行编辑')
        }
      },
      refresh () {
        this.handleSizeChange()
      },
      // 新增/修改 元事件
      submitForm (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
          //   switch (this.ruleForm.meta_type) {
          //     case '数据元':
          //     this.ruleForm.meta_type = 'elem'
          //     break
          //     case '物理模型':
          //     this.ruleForm.meta_type = 'table'
          //     break
          //     case '系统标识':
          //     this.ruleForm.meta_type = 'sys'
          //     break
          //     case '应用元':
          //     this.ruleForm.meta_type = 'colume'
          //     break
          //     case '业务指标':
          //     this.ruleForm.meta_type = 'tgt'
          //     break
          //  }
            const insertdata = {
              meta_table_name: this.ruleForm.meta_table_name,
              meta_id_fld: this.ruleForm.meta_id_fld,
              meta_id_fix: this.ruleForm.meta_id_fix,
              meta_table_name_cns: this.ruleForm.meta_table_name_cns,
              meta_type: this.ruleForm.meta_type,
              busi_desc: this.ruleForm.busi_desc,
              tech_desc: this.ruleForm.tech_desc,
              meta_name_fld: this.ruleForm.meta_name_fld
            }
            if (this.addRoleDialogMode === 'ADD') {
              // 新增接口
              ajax.post(api.INSERT_META_INDEX, insertdata).then((res) => {
                this.handleSizeChange()
                  this.$message({
                  type: 'success',
                  message: '添加成功!'
              })
                })
            } else if (this.addRoleDialogMode === 'MODIFY') {
              // 修改接口
              ajax.post(api.UPDATE_META_INDEX, insertdata).then((res) => {
                this.ruleForm = {}
                this.handleSizeChange()
                  this.$message({
                  type: 'success',
                  message: '编辑成功!'
            })
              })
            }
            this.addRoleDialogVisiable = false
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
  #meta-index
    height 100%
    position relative
    .table-manage-box
      height 100%
      padding 10px 0px 20px 10px
      .header-tool-bar
        line-height 50px
        vert-align middle
        background-color #FFFFFF
        margin-bottom 3px
        .condition-query
          width 300px
          float right
          margin-right -10px
        .el-input--mini
          width 200px
        .el-button--danger
          background-color #016DFB
          border-color #016DFB
        .dialog-input
          width 210px
          height 30px
          border-radius 4px
          border 1px solid #D8DCE5
      .main-table-box
        padding 0px 0px
        height 100%
        background-color #fff
        .el-table
          padding-bottom 74px
        .el-pagination
          z-index 5
          height 36px
          background-color #fff
          position absolute
          width 100%
          bottom 0px
          padding-bottom 4px
          border-top solid 1px #f4f4f4
    .el-dialog
      width 900px
      padding-right 30px
      .el-dialog__body
        margin 0 auto
        padding 20px 20px
      .el-select
        width 253px
        height 41px
        .btn-center
          text-align center
    #dialogbtn
        margin-left 440px
</style>
